const path = require('path');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const baseConfig = require('./webpack.config.base');

const isDev = process.env.NODE_ENV === 'development';

const config = webpackMerge(baseConfig, {
  target: 'web',
  mode: 'development',
  // devtool: 'inline-source-map',
  // watch: true, // 启用监听文件变化
  entry: {
    app: path.join(__dirname, './src/app/app.js'),
  },
  output: {
    filename: '[name]-[hash:6].js',
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './src/static/template/index.html'),
      favicon: path.resolve(__dirname, './src/static/favicon.ico'),
      filename: './index.html',
    }),
    new HtmlWebpackPlugin({
      template: '!!ejs-compiled-loader!' + path.resolve(__dirname, './src/static/template/server.tempalte.ejs'),
      favicon: path.resolve(__dirname, './src/static/favicon.ico'),
      filename: './server.ejs',
    })
  ]
})

if(isDev) {
  config.entry = {
    app: [
      'react-hot-loader/patch',
      path.join(__dirname, './src/app/app.js'),
    ]
  }
  config.devServer = {
    host: '0.0.0.0',
    port: 7000,
    contentBase: path.join(__dirname, `./dist`),
    publicPath: '/public',
    historyApiFallback: { // 解决单页应用刷新404
      index: '/public/index.html'
    },
    hot: true,
    overlay: {
      errors: true // 显示网页错误黑色弹窗
    }
  },
  config.plugins.push(new webpack.HotModuleReplacementPlugin())
}

module.exports = config;
